package org.dalmatian.common.tenant.config;

import cn.dev33.satoken.dao.SaTokenDao;
import org.dalmatian.common.redis.cache.CacheManager;
import org.dalmatian.common.redis.config.properties.RedissonProperties;
import org.dalmatian.common.tenant.core.TenantSaTokenDao;
import org.dalmatian.common.tenant.handle.PlusTenantLineHandler;
import org.dalmatian.common.tenant.manager.TenantCacheManager;
import org.dalmatian.common.tenant.properties.TenantProperties;
import org.dalmatian.common.tenant.supplier.TenantRedissonClientSupplier;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.redisson.api.RedissonClient;


/**
 * 租户配置类
 *
 * @author Lion Li
 */

@Condition(onProperty = "${tenant.enable} == true")
@Configuration
@Slf4j
public class TenantConfig {


    /**
     * 多租户插件
     */
    @Bean
    public TenantLineInnerInterceptor tenantLineInnerInterceptor(
        @Inject TenantProperties tenantProperties) {
        return new TenantLineInnerInterceptor(new PlusTenantLineHandler(tenantProperties));
    }

    @Inject
    private RedissonProperties redissonProperties;


    @Bean
    public RedissonClient redissonClient(){
        log.info(" redisson-tenant config init");
        return new TenantRedissonClientSupplier(redissonProperties).get();
    }


    /**
     * 多租户鉴权dao实现
     */
    @Bean
    public SaTokenDao tenantSaTokenDao() {
        return new TenantSaTokenDao();
    }

    @Bean
    @Condition(onProperty = "${tenant.enable} == true")
    public CacheManager tenantCacheManager() {
        log.info(" redisson-tenant-cache init");
        return new TenantCacheManager();
    }

}
